//
//  WBHomeViewController.swift
//  weibo
//
//  Created by 李海超 on 2017/5/21.
//  Copyright © 2017年 lihaichao. All rights reserved.
//



















import UIKit

private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {
    
    //视图模型
    private lazy var statusViewModel = WBStatusViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //取消自动缩进
        
        loadData()
        
    }
    
    //加载数据
    override func loadData() {
        
        print("准备刷新数据，\(self.statusViewModel.staticList.last?.text)")
        
        statusViewModel.loadStatus(pullup: self.isPullUp) {(isSuccess,hasMoreStatus) in
            
            //恢复上拉刷新标记
            self.isPullUp = false
            
            self.refreshControl?.endRefreshing()
            
            if hasMoreStatus{
                self.tableView?.reloadData()
            }
        }
        
}
    
    
    override func setupTaleView() {
        super.setupTaleView()
        //使用便利构造函数创建UIBarButtonItem
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends))
        
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }

    
    
    
    
    @objc private func showFriends(){
        
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.staticList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        
        cell?.textLabel?.text = statusViewModel.staticList[indexPath.row].text
        
        return cell ?? UITableViewCell()
    }
}


